<?php
// +----------------------------------------------------------------------
// | LanCMS [ 永久网络工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019 http://www.19www.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Lajox <lajox@19www.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 日志首页
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Apilog as ApilogModel;
use app\common\controller\Adminbase;
use think\Db;

class Apilog extends Adminbase
{
    /**
     * @var ApilogModel
     */
    protected $_model;

    protected function initialize()
    {
        parent::initialize();
        $this->_model = new ApilogModel();
    }

    /**
     * 日志首页
     */
    public function index()
    {
        if ($this->isJson()) {
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 1);
            $map = $this->buildparams();
            $order = $this->bulidorder(['id' => 'desc']);
            $data = $this->_model
                ->where($map)
                ->page($page, $limit)
                ->order($order)
                ->select();
            $total = $this->_model->where($map)->count();
            return self::jsonSuccess($data, ["count" => $total]);
        }
        return $this->fetch();
    }

    /**
     * 删除一个月前的日志
     */
    public function deletelog()
    {
        if ($this->_model->deleteAMonthago()) {
            $this->success('删除日志成功！');
        } else {
            $this->error('删除日志失败！');
        }
    }

    /**
     * 查看日志详情
     */
    public function view($id)
    {
        config('default_ajax_return', 'html');
        $info = $this->_model->get(intval($id));
        $this->assign('info', $info);
        return $this->fetch();
    }

}
